package Leetcode;

import java.util.Deque;
import java.util.LinkedList;

class Solution_150 {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for (String tokenString : tokens) {
            if (!"+".equals(tokenString) && !"-".equals(tokenString) &&
                    !"*".equals(tokenString) && !"/".equals(tokenString)) {
                stack.push(Integer.parseInt(tokenString));
            } else {
                int num1 = stack.pop();
                int num2 = stack.pop();
                if ("+".equals(tokenString))
                    stack.push(num1 + num2);
                else if ("-".equals(tokenString))
                    stack.push(num2 - num1);
                else if ("*".equals(tokenString))
                    stack.push(num1 * num2);
                else
                    stack.push(num2 / num1);
            }
        }
        return stack.pop();
    }
}